<template>
	<view>
		<iframe v-if="!good" style="width: 100%;height: 420rpx;" :src="detail.
iframe+'&high_quality=1'" scrolling="no"
		 border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>
		<video v-else style="width: 100%;height: 450rpx;" :src="detail.video" :poster="detail.cover" controls></video>
		<view v-if="detail" class="cu-list menu card-menu margin-top margin-bottom sm-border ">
			<view class="cu-item" >
				<view class="content" style="padding-top: 20rpx;">
					<view class="round light text-olive" v-if="detail.name">{{detail.name}}</view>
				</view>
			</view>
			<view class="cu-item">
				<view class="content" style="padding-bottom: 20rpx;padding-top: 20rpx;">
					<view class="round light text-olive">{{detail.desc}}</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">创作者</view>
				<view class="">
					<view class='cu-tag  round bg-orange' v-if="detail.author&&detail.author.name">{{detail.author.name}}</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">标签</view>
				<view class="">
					<view v-for="(item,index) in detail.tags" class='cu-tag  round bg-olive' :key="index" v-if="detail.tags&&detail.tags.length>0">{{item}}</view>
				</view>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">清晰度 <view class='cu-tag sm round bg-orange ml-1'>{{good?'超清':'标准'}}</view>
				</view>
				<switch @change="SwitchA" :class="good?'checked':''" :checked="good?true:false"></switch>
			</view>
			<view class="cu-item">
				<view class="content" style="padding-bottom: 20rpx;">
					<view class="round light text-bold text-red">建议WIFI下观看</view>
				</view>
			</view>
		</view>
		<view class="flex  align-center justify-center" style="width: 100rpx;height: 100rpx;position: fixed;bottom: 100rpx;right:30rpx;"
		 @click="onBack">
			<button class="cu-btn three-shadow cuIcon">
				<text class="cuIcon-home text-orange" style="font-size: 40rpx;"></text></button>
		</view>
		<view class="flex  align-center justify-center" style="width: 100rpx;height: 100rpx;position: fixed;bottom: 180rpx;right:30rpx;"
		 @click="show=true">
			<button class="cu-btn three-shadow cuIcon">
				<text class="cuIcon-post text-orange" style="font-size: 40rpx;"></text></button>
		</view>
		<view class="cu-modal" :class="show?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">请输入密码</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					<input placeholder="请输入密码-ggb专用" v-model="password"></input>
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-green " @tap="hideModal">取消</button>
						<button class="cu-btn bg-green margin-left" @tap="confirmModal">确定</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database()
	export default {
		data() {
			return {
				good: false,
				detail: {},
				pageIndex: 0,
				id: 0,
				show: false,
				password: '',
			}
		},
		onLoad(options) {
			let page = getCurrentPages()
			this.pageIndex = page.length
			if (!options.id) {
				uni.showToast({
					title: '参数错误'
				});
				this.onBack()
			}
			this.id = options.id

			this.loadData()
		},
		onShow() {
			let page = getCurrentPages()
			this.pageIndex = page.length
			if(this.detail){
				let data = {
					title: this.detail.name,
					desc: this.detail.desc,
					imgUrl: this.detail.cover,
					dataUrl: this.detail.src,
					type: 'video'
				}
				this.initShare(data)
			}
		},
		onPullDownRefresh() {
			this.loadData()
		},
		methods: {
			SwitchA(e) {
				let status = e.detail.value
				if (status && !this.detail.video) {
					return uni.showToast({
						title: '高清资源未上传',
						icon: 'none'
					});
				}
				this.good = status
				uni.showToast({
					title: '切换成功',
					icon: 'none'
				});
			},
			confirmModal() {
				// 验证密码
				this.show = false
				uniCloud.callFunction({
					name: 'checkPassword',
					data: this.password
				}).then(res => {
					this.password = ""
					if (res.result.data) {
						uni.navigateTo({
							url: '../publish/publish?id=' + this.id
						})
					} else {
						uni.showToast({
							title: res.result.msg,
							icon: 'none'
						});
					}
				})
			},
			hideModal() {
				this.show = false
			},
			loadData() {
				uni.showLoading({
					title: '加载中...'
				})
				db.collection('vlog').doc(this.id).get().then(res => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
					if (res.success && res.result.data && res.result.data.length > 0) {
						this.detail = res.result.data[0]
						let data = {
							title: this.detail.name,
							desc: this.detail.desc,
							imgUrl: this.detail.cover,
							dataUrl: this.detail.src,
							type: 'video'
						}
						this.initShare(data)
						uni.setNavigationBarTitle({
							title: this.detail.name
						})
					} else {
						this.onBack()
					}
				}).catch(err => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
					uni.showToast({
						title: '请求失败',
						icon: 'none'
					});
				})
			},
			onBack() {
				if (this.pageIndex <= 1) {
					uni.navigateTo({
						url: '/pages/index/index'
					})
				} else {
					uni.navigateBack({
						delta: 1
					})
				}
			},
			initShare(data) {
				if (this.$jwx && this.$jwx.isWechat()) {
					this.$jwx.initJssdk(data)
				}
			}
		}
	}
</script>

<style>
	.three-shadow {
		box-shadow: 0 20rpx 20rpx -20rpx rgba(51, 51, 51, 0.1),
			-10px 0px 20rpx -20rpx rgba(51, 51, 51, 0.1),
			10px 0px 20rpx -20rpx rgba(51, 51, 51, 0.1);

	}
</style>
